package com.uade.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.uade.business.BusinessDelegate;
import com.uade.business.BusinessDelegateImpl;
import com.uade.model.Usuario;

/**
 * Servlet implementation class RegistrarUsuarioSrv
 */
public class RegistrarUsuarioSrv extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistrarUsuarioSrv() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			response.setContentType("text/html");
	
		try {
			
			String nombre = request.getParameter("nombre");
			String apellido = request.getParameter("apellido");
			String email = request.getParameter("email");
			String password = request.getParameter("password");
			String coorX= request.getParameter("coordenadasX");
			String coorY= request.getParameter("coordenadasY");
			Usuario usr = new Usuario();
			
			usr.setNombre(nombre);
			usr.setApellido(apellido);
			usr.setEmail(email);
			usr.setPassword(password);
			usr.setTipo("usuario");
			if(coorX!=null)
				usr.setCoordenadaX(Double.parseDouble(coorX));
			if(coorY!=null)
				usr.setCoordenadaY(Double.parseDouble(coorY));
			
			BusinessDelegate busDel = BusinessDelegateImpl.getInstance();
			busDel.altaUsuario(usr);
			
			RequestDispatcher rd = request.getRequestDispatcher("/Login.jsp");
			rd.forward(request, response);
					
		}
		catch (Exception e) {
			e.printStackTrace();
			
			String msg = e.toString();
			request.setAttribute("mensaje", msg);
			RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/Error.jsp");
			dispatcher.forward(request, response);
		}
	}

}
